package com.min.generator.entity;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.min.generator.config.GeneratorConfig;
import com.min.generator.utils.NameUtils;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Data
@Accessors(chain = true)
public class TemplateData {

    private String author;
    private String email;
    private String datetime;
    private String moduleName;
    private String packageName;
    private String mainPath;
    private List<Column> columns;
    private Column primaryKey;
    private String comments;
    private String className;
    private String pathName;
    private String tableName;

    /**
     * 数据库新增数据的ID
     **/
    private List<Long> idList;

    public static TemplateData builder(GeneratorConfig c, Table t) {
        return new TemplateData()
                .setAuthor(c.getAuthor())
                .setEmail(c.getEmail())
                .setDatetime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"))
                .setModuleName(c.getModuleName())
                .setPackageName(c.getPackageName())
                .setMainPath(c.getMainPath())
                .setComments(t.getComment())
                .setTableName(t.getName())
                .setPrimaryKey(t.getPrimaryKey())
                .setColumns(t.getColumns())
                .setClassName(NameUtils.tableNameToClassName(t.getName(), c.getTablePrefix()))
                .setPathName(NameUtils.tableNameToClassName(t.getName(), c.getTablePrefix()).toLowerCase());

    }

    public static Map mapBuilder(GeneratorConfig c, Table t) {
        Gson gson = new GsonBuilder().create();
        String s = gson.toJson(builder(c, t));
        return gson.fromJson(s, Map.class);
    }

}
